package dao.impl;

import dao.BaseDao;
import dao.MetaverseDao;
import entity.MetaverseProduct;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MetaverseDaoImpl extends BaseDao implements MetaverseDao {
    //查询全部
    @Override
    public List<MetaverseProduct> Select() {
        List<MetaverseProduct> list = new ArrayList<>();
        try {
            conn = getConnection();
            pstmt = conn.prepareStatement("SELECT`id`,`product_name`,`product_price`,`product_desc`,`flag` from metaverse_product");
            rs = pstmt.executeQuery();
            while (rs.next()){
                MetaverseProduct metaverseProduct = new MetaverseProduct();
                metaverseProduct.setId(rs.getInt(1));
                metaverseProduct.setProductName(rs.getString(2));
                metaverseProduct.setProductPrice(rs.getDouble(3));
                metaverseProduct.setProductDesc(rs.getString(4));
                metaverseProduct.setFlag(rs.getInt(5));
                list.add(metaverseProduct);
            }
        }catch (SQLException ex){
            ex.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return list;
    }

    @Override
    public int delete(int id) {
        int result = -1;
        try{
            conn =super.getConnection();
            pstmt = conn.prepareStatement("delete from `metaverse_product` where id = ?");
            pstmt.setInt(1,id);
            result = pstmt.executeUpdate();
        }catch (SQLException ex){
            ex.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return result;
    }
}
